package com.atlzk.group.user.controller;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.*;

import com.atlzk.group.user.entity.UserEntity;
import com.atlzk.group.user.service.UserService;
import com.atlzk.common.utils.PageUtils;
import com.atlzk.common.utils.R;


/**
 * 用户表
 *
 * @author liuzekun
 * @email 2803587841@gmail.com
 * @date 2021-06-18 17:02:56
 */
@RestController
@RequestMapping("user/user")
@CrossOrigin
@EnableDiscoveryClient
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("user:user:list")
    public R list(@RequestParam Map <String, Object> params) {
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{umsUserId}")
    //@RequiresPermissions("user:user:info")
    public R info(@PathVariable("umsUserId") Long umsUserId) {
        UserEntity user = userService.getById(umsUserId);

        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("user:user:save")
    public R save(@RequestBody UserEntity user) {
        userService.save(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("user:user:update")
    public R update(@RequestBody UserEntity user) {
        userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("user:user:delete")
    public R delete(@RequestBody Long[] umsUserIds) {
        userService.removeByIds(Arrays.asList(umsUserIds));

        return R.ok();
    }

    /**
     * 登录
     * @param userEntity
     * @return
     */
    @RequestMapping("/login")
    public R login(@RequestBody UserEntity userEntity) {
        UserEntity login = userService.getById(userEntity.getUmsUserId());
        if (login.getUmsUserPassword().equals(userEntity.getUmsUserPassword())) {
            System.out.println("登录成功");
            return R.ok("登录成功");
        } else {
            System.out.println("登录失败");
            return R.error(300,"登录失败");
        }

    }

    @RequestMapping("/findUserByUserId")
    public UserEntity findUserByUserId(@RequestParam("userId") Long userId) {
        UserEntity user = userService.getById(userId);
        return user;
    }
}
